package org.project.d.network.online;

import com.google.common.collect.Maps;

import java.util.Map;
import java.util.Set;

/**
 * @author lin
 * @version V1.0
 * @description
 * @date 2020/10/16 3:55 下午
 */
public class MapLocalOnlineManager implements LocalOnlineManager {
    Map<String, OnlineInfo> cache= Maps.newConcurrentMap();
    Map<String,String> ipMapId = Maps.newConcurrentMap();


    @Override
    public boolean online(OnlineInfo onlineInfo) {
        if (checkId(onlineInfo)){
            cache.put(getOnlineUserListKey(onlineInfo),onlineInfo);
            ipMapId.put(onlineInfo.getClineAddress(),onlineInfo.getUserId());
            return true;
        }
        return false;
    }
    private String getOnlineUserListKey(OnlineInfo onlineInfo) {
        return CacheKeys.getOnlineUserListKey(onlineInfo.getUserId());
    }
    @Override
    public boolean offline(OnlineInfo onlineInfo) {
        if (checkId(onlineInfo)){
            cache.remove(getOnlineUserListKey(onlineInfo));
            ipMapId.remove(onlineInfo.getClineAddress());
            return true;
        }else if (onlineInfo!=null&&onlineInfo.getClineAddress()!=null){
            String userId = ipMapId.remove(onlineInfo.getClineAddress());
            onlineInfo.setUserId(userId);
            cache.remove(getOnlineUserListKey(onlineInfo));
            return true;
        }
        return false;
    }

    @Override
    public boolean kickline(OnlineInfo onlineInfo) {
        if (checkId(onlineInfo)){
            cache.remove(getOnlineUserListKey(onlineInfo));
            ipMapId.remove(onlineInfo.getClineAddress());
            return true;
        }
        return false;
    }

    @Override
    public OnlineInfo selectOnlineInfo(String id) {
        return cache.get(CacheKeys.getOnlineUserListKey(id));
    }

    @Override
    public boolean update(OnlineInfo onlineInfo) {
        if (checkId(onlineInfo)){
            if (onlineInfo.getOnlineState()== OnlineState.OFF_LINE.getState()){
                return offline(onlineInfo);
            }
            if (onlineInfo.getOnlineState()== OnlineState.ON_LINE.getState()){
                return online(onlineInfo);
            }
            cache.replace(getOnlineUserListKey(onlineInfo),onlineInfo);
            return true;
        }
        return false;
    }

    @Override
    public void keepLive(String id) {

    }

    @Override
    public void offlineAll() {
        cache.clear();
        ipMapId.clear();
    }

    private boolean checkId(OnlineInfo onlineInfo) {
        return onlineInfo!=null&&onlineInfo.getUserId()!=null;
    }

    @Override
    public OnlineInfo selectOnlineInfoByIp(String ip) {
        String id = ipMapId.get(ip);
        if (id!=null){
            return cache.get(CacheKeys.getOnlineUserListKey(id));
        }
        return null;
    }

    @Override
    public Set<String> getCacheKeys() {
        return cache.keySet();
    }

    @Override
    public String getId(String ip) {
        return ipMapId.get(ip);
    }
}
